package com.iflytek.uoamp.notify.core.protocol;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.iflytek.uoamp.common.notify.enumerate.ContentType;
import com.iflytek.uoamp.common.notify.obj.BaseInfo;
import com.iflytek.uoamp.common.notify.obj.BizInfo;
import com.iflytek.uoamp.common.notify.obj.Content;
import com.iflytek.uoamp.common.notify.obj.MonitorInfo;
import com.iflytek.uoamp.common.notify.obj.Notification;
import com.iflytek.uoamp.common.notify.obj.NotifyInfo;
import com.iflytek.uoamp.common.notify.util.NotifyUtils;

public class TemplateNotification extends Notification {
	
    private static final long serialVersionUID = -7370060224123829857L;
    
    private List<Content> content;
    
    @JSONField(name = "plugin_nodes")
    public List<TemplatePluginNode> pluginNodes;

    public List<Content> getContent() {
        return content;
    }
    
    public void setContent(List<Content> content) {
        this.content = content;
    }
    
    public List<TemplatePluginNode> getPluginNodes() {
        return pluginNodes;
    }
    
    public void setPluginNodes(List<TemplatePluginNode> pluginNodes) {
        this.pluginNodes = pluginNodes;
    }
    
    public NotifyInfo toNotifyInfo(List<?> contents) {
        NotifyInfo info = new NotifyInfo();
        
        if(contents == null) {
            return info;
        }
        
        info.setNotifyHost(getNotifyHosts(contents));
        info.setNotifyName(getNotifyName((Content)contents.get(0)));
            
        info.setState(state);
        GregorianCalendar c = new GregorianCalendar();
        c.setTimeInMillis(System.currentTimeMillis());
        info.setCreatedTime(c.getTime());
        
        return info;
    }
    
    private List<String> getNotifyHosts(List<?> contents) {
    	List<String> hosts = new ArrayList<String>();
    	
    	for(Object item : contents) {
    		Content content = (Content)item;
    		MonitorInfo monitorInfo = content.getMonitorInfo();
    		
    		if(monitorInfo != null) {
    			hosts.add(getNotifyHost(monitorInfo));
    		}
    	}
    	
    	return hosts;
    }
    
    private String getNotifyHost(MonitorInfo monitorInfo) {
        BaseInfo baseInfo = monitorInfo.getBaseInfo();
        BizInfo bizInfo = monitorInfo.getBizInfo();
        ContentType type = ContentType.getType(pack_type);
        
        /**
         * 由于业务邮件也有可能使用LOG型模版，所以此处如果包类型为BIZ或LOG则判断bizInfo是否为空
         * 如果不为空，将notify_host设置为监控点名称，否则设置为IP地址
         */
        if((type == ContentType.BIZ || type == ContentType.LOG) && bizInfo != null) {
            return StringUtils.isNotEmpty(baseInfo.getIP()) ? baseInfo.getIP() : bizInfo.getMonitorptName();
        } else if(baseInfo != null) {
            String ip = baseInfo.getIP();
           return ip == null ? "" : ip;
        } else {
            return "";
        }
    }
    
    private String getNotifyName(Content content) {
        String output = content.getOutput();
        
        return output == null ? "" : NotifyUtils.getAttributeValue(output, "monitorname");
    }
}
